package com.hkw.servlet;

import com.hkw.model.ClassRoom;
import com.hkw.service.IClassRoomService;
import com.hkw.service.impl.ClassRoomServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 教室查询请求的Servlet
 */
@WebServlet(name = "SearchClassRoomServlet",urlPatterns = "/searchClassRoom")
public class SearchClassRoomServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String grade = request.getParameter("grade");
        String classTeaName = request.getParameter("classTeaName");
        ClassRoom classRoom =new ClassRoom(name,grade,classTeaName,null);
        IClassRoomService classRoomService = new ClassRoomServiceImpl();
        //调用service服务
        List<ClassRoom> classRoomByClassRoom = classRoomService.findClassRoomByClassRoom(classRoom);
        if(classRoomByClassRoom != null){
            request.setAttribute("classList",classRoomByClassRoom);
            request.setAttribute("reaPage",1);
            request.getRequestDispatcher("classroom.jsp").forward(request,response);
        }else{
            //查找失败，往request里设值
            request.setAttribute("searcherror","没有符合条件的信息!");
            //跳转
            request.getRequestDispatcher("classroom.jsp").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
